package com.yuanrui.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yuanrui.auth.application.convert.AuthRoleConverter;
import com.yuanrui.auth.application.dto.AuthRoleDTO;
import com.yuanrui.auth.common.entity.Result;
import com.yuanrui.auth.domain.entity.AuthRoleBO;
import com.yuanrui.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户角色控制器
 */
@RestController
@Slf4j
@RequestMapping("/role/")
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 添加角色
     */
    @RequestMapping("add")
    public Result<Boolean> addRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("RoleController.add：{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名称不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convert(authRoleDTO);
            Boolean addRole = authRoleDomainService.addRole(authRoleBO);
            return Result.ok(addRole);
        } catch (Exception e) {
            log.error("RoleController.add.error:{}", e.getMessage(), e);
            return Result.fail("添加角色失败");
        }
    }

    /**
     * 修改角色
     */
    @RequestMapping("update")
    public Result<Boolean> updateRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("RoleController.update：{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色ID不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convert(authRoleDTO);
            Boolean updateRole = authRoleDomainService.updateRole(authRoleBO);
            return Result.ok(updateRole);
        } catch (Exception e) {
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("修改角色失败");
        }
    }

    /**
     * 删除角色
     */
    @RequestMapping("delete")
    public Result<Boolean> deleteRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("RoleController.delete：{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色ID不能为空");
            AuthRoleBO authRoleBO = AuthRoleConverter.INSTANCE.convert(authRoleDTO);
            Boolean deleteRole = authRoleDomainService.deleteRole(authRoleBO);
            return Result.ok(deleteRole);
        } catch (Exception e) {
            log.error("RoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除角色失败");
        }
    }
}
